<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/23
 * Time: 15:00
 */

namespace App\Repositories\Manage;

use Illuminate\Support\Facades\DB;


class RoleRepo
{
    protected $roleTable = 'manage_role';
    protected $roleMenuTable = 'manage_role_menu';

    /**
     * 获取角色列表
     */
    public function getRoleList(){
       return  DB::table($this->roleTable)->get();
    }

    /**获取单个角色数据
     * @param $where        string
     * @param $whereArr     array
     * @return object
     */
    public function getRole($where,$whereArr){
        return  DB::table($this->roleTable)->whereRaw($where,$whereArr)->first();
    }



    public function addRole($name,$sort){

        $data=[
            'name'=>$name,
            'sort'=>$sort
        ];
        return  DB::table($this->roleTable)->insertGetId($data);
    }


    public function updateRole($id,$name,$sort){

        $data=[
            'name'=>$name,
            'sort'=>$sort
        ];
        return  DB::table($this->roleTable)->where('id',$id)->update($data);
    }

    /**删除角色
     * @param $id   int
     * @return int
     */

    public function delRole($id){

        return  DB::table($this->roleTable)->where('id',$id)->delete();
    }


    /**获取角色对应菜单列表
     * @param $where       string
     * @param $whereRaw    array
     * @return object
     */
    public function getRoleMenuList($where,$whereRaw){
        return  DB::table($this->roleMenuTable)->select('menu_id','power')->whereRaw($where,$whereRaw)->get();
    }


    /**获取单个角色对应菜单数据
     * @param $where       string
     * @param $whereRaw    array
     * @return object
     */
    public function getRoleMenu($where,$whereRaw){
        return  DB::table($this->roleMenuTable)->select('power')->whereRaw($where,$whereRaw)->first();
    }


    public function addRoleMenu($roleId,$menuId,$power){
        $len=$this->menuModuleCount('role_id = ? and menu_id = ?',[$roleId,$menuId]);
        if($len>0){
            $data=[
                'power'=>$power
            ];
            return  DB::table($this->roleMenuTable)->whereRaw('role_id = ? and menu_id = ?',[$roleId,$menuId])->update($data);
        }else{
            $data=[
                'role_id'=>$roleId,
                'menu_id'=>$menuId,
                'power'=>$power
            ];
            return  DB::table($this->roleMenuTable)->insertGetId($data);
        }
    }


    public function menuModuleCount($where,$whereArr){

        return  DB::table($this->roleMenuTable)->whereRaw($where,$whereArr)->count();
    }


    /**获取菜单ID数组
     * @param $where        string
     * @param $whereArr     array
     * @return array
     */

    public function getMenuId($where,$whereArr)
    {
        return DB::table($this->roleMenuTable)->whereRaw($where,$whereArr)->pluck('menu_id')->all();
        //lists 5.3以下使用   5.3及以上用pluck返回集合  ->all()转为数组
    }


    public function delRoleMenu($where,$whereArr)
    {
        return  DB::table($this->roleMenuTable)->whereRaw($where,$whereArr)->delete();
    }


    /**获取角色id
     * @param $where        string
     * @param $whereArr     array
     * @return int|null
     */

    public function getRoleId($where,$whereArr)
    {
        $data=DB::table('manage_user_role')->whereRaw($where,$whereArr)->first();
        if(!empty($data)){
            return $data->role_id;
        }else{
          return null;
        }
    }




}